package com.zkhy.gz.hhg;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.tencent.mm.opensdk.modelmsg.GetMessageFromWX;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXTextObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.zkhy.gz.hhg.R;

public class WxDemoMainActivity extends AppCompatActivity {

    private IWXAPI api;

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_activity_main);

        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
        api.registerApp(Constants.APP_ID);

        findViewById(R.id.sendReqBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String text = "11111111111111111111111";

                WXTextObject textObj = new WXTextObject();
                textObj.text = text;

                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = textObj;
                // msg.title = "Will be ignored";
                msg.description = text;
                msg.mediaTagName = "我是mediaTagName啊";

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = buildTransaction("text");
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneSession;

                api.sendReq(req);
            }
        });

        findViewById(R.id.sendReq2Btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = "222222222222222222222";

                //初始化一个 WXTextObject 对象，填写分享的文本内容
                WXTextObject textObj = new WXTextObject();
                textObj.text = text;

                //用 WXTextObject 对象初始化一个 WXMediaMessage 对象
                WXMediaMessage msg = new WXMediaMessage();
                msg.mediaObject = textObj;
                msg.description = text;

                SendMessageToWX.Req req = new SendMessageToWX.Req();
                req.transaction = String.valueOf(System.currentTimeMillis());  //transaction字段用与唯一标示一个请求
                req.message = msg;
                req.scene = SendMessageToWX.Req.WXSceneTimeline;

                //调用api接口，发送数据到微信
                api.sendReq(req);

            }
        });

        findViewById(R.id.sendRespBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 初始化一个 WXTextObject 对象
                WXTextObject textObj = new WXTextObject();
                textObj.text = "5555";

                // 用 WXTextObject 对象初始化一个 WXMediaMessage 对象
                WXMediaMessage msg = new WXMediaMessage(textObj);
                msg.description = "66666666";

                // 构造一个Resp
                GetMessageFromWX.Resp resp = new GetMessageFromWX.Resp();
                // 将req的transaction设置到resp对象中，其中bundle为微信传递过来的Intent所带的内容，通过getExtras()方法获取
//                resp.transaction = new GetMessageFromWX.Req(bundle).transaction;
                resp.message = msg;

                //调用api接口，发送数据到微信
                api. sendResp (resp) ;
            }
        });

        findViewById(R.id.loginWxBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin();
            }
        });
    }

    private void doLogin() {
        // send oauth request
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
//        req.state = "wechat_sdk_demo_test";

        api.sendReq(req);
    }


}